package co.com.umobile.gui;

import android.app.ActionBar;
import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Intent;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import co.com.umobile.R;
import co.com.umobile.application.GlobalState;
import co.com.umobile.persistence.UmobileProvider;

public class SubjectDetailActivity extends Activity implements LoaderManager.LoaderCallbacks<Cursor> {

	/** globalState is used to transfer data to other activity. */
	private GlobalState globalState;

	/** the custom list adapter. */
	private SimpleCursorAdapter listAdapter;

	/** the screen list view. */
	private ListView listView;
	
	private String subjectCode;

	/** colors of the list. */
	private int[] colors = new int[] { R.color.White, R.color.gray_zebra };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		getWindow().requestFeature(Window.FEATURE_ACTION_BAR);

		globalState = (GlobalState) getApplication();

		subjectCode = getIntent().getExtras().getString("subjectCode");
		
		ActionBar actionBar = getActionBar();
		actionBar.setTitle(R.string.subject_list_title);

		setContentView(R.layout.subject_detail_list_layout);

		int[] toItem = { R.id.TextViewPeriod, R.id.TextViewGrade1, R.id.TextViewGrade2, R.id.TextViewGrade3, R.id.textViewGrade4,
				R.id.textViewFinalGrade, R.id.textViewDefinitiveGrade };
		String[] fromItem = { "subjectPeriod", "grade1", "grade2", "grade3", "grade4", "finalGrade", "definitiveGrade" };

		listAdapter = new SimpleCursorAdapter(SubjectDetailActivity.this, R.layout.row_subject_detail, null, fromItem, toItem) {

			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				int colorPos = position % colors.length;

				View view = super.getView(position, convertView, parent);
				view.setBackgroundResource(colors[colorPos]);
				return view;
			}

		};

		listView = (ListView) findViewById(R.id.listViewDetailInserted);
		listView.setItemsCanFocus(true);
		listView.setAdapter(listAdapter);

		getLoaderManager().initLoader(0, null, this);

	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}

	public Loader<Cursor> onCreateLoader(int id, Bundle args) {
		return new CursorLoader(SubjectDetailActivity.this, UmobileProvider.CONTENT_URI_SUBJECT__DETAIL_LIST, null, subjectCode, null, null);
	}

	public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
		listAdapter.swapCursor(data);
	}

	public void onLoaderReset(Loader<Cursor> loader) {
		listAdapter.swapCursor(null);
	}

}
